package com.gl.goods.httpclint.response;



import android.content.Intent;

import com.gl.goods.base.MyApplication;
import com.gl.goods.httpclint.MyOkHttp;
import com.gl.goods.httpclint.util.LogUtils;
import com.gl.goods.ui.login.activity.LoginActivity;
import com.gl.goods.ui.main.activity.MainActivity;
import com.gl.library.tool.RxSPTool;
import com.gl.library.tool.RxTool;
import com.hjq.toast.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.IOException;

import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * json类型的回调接口
 *
 */
public abstract class JsonResponseHandler implements IResponseHandler {

    private int code;
    private String message;

    @Override
    public final void onSuccess(final Response response) {
        ResponseBody responseBody = response.body();
        String responseBodyStr = "";

        try {
            responseBodyStr = responseBody.string();
        } catch (IOException e) {
            e.printStackTrace();
            LogUtils.e("onResponse fail read response body");

            MyOkHttp.mHandler.post(new Runnable() {
                @Override
                public void run() {
                    onFailure(response.code(), "fail read response body");
                }
            });
            return;
        } finally {
            responseBody.close();
        }

        final String finalResponseBodyStr = responseBodyStr;

        try {
            final Object result = new JSONTokener(finalResponseBodyStr).nextValue();
            if(result instanceof JSONObject) {

                MyOkHttp.mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            code = Integer.valueOf(((JSONObject) result).getString("code"));
                            message = ((JSONObject) result).getString("message");
                            if (code == 1){
                                onSuccess(response.code(), (JSONObject) result);
                            } else if (code == -1 || code == 3) {
                                ToastUtils.show(message);
                                MyApplication.getInstance().logout();
                                RxSPTool.putInt(RxTool.getContext(),"id", 0);
                                RxSPTool.putString(RxTool.getContext(), "token", "");
                                Intent i = new Intent(RxTool.getContext(), MainActivity.class);
                                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                RxTool.getContext().startActivity(i);
                            } else {
                                LogUtils.e("onResponse fail parse jsonobject, body=" + message);
                                MyOkHttp.mHandler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        onFailure(code, message);
                                    }
                                });
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            LogUtils.e("onResponse fail parse jsonobject, body=" + finalResponseBodyStr);
                            MyOkHttp.mHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    onFailure(response.code(), "fail parse jsonobject, body=" + finalResponseBodyStr);
                                }
                            });
                        }


                    }
                });
            } else if(result instanceof JSONArray) {
                MyOkHttp.mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        onSuccess(response.code(), (JSONArray) result);
                    }
                });
            } else {
                LogUtils.e("onResponse fail parse jsonobject, body=" + finalResponseBodyStr);
                MyOkHttp.mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        onFailure(response.code(), "fail parse jsonobject, body=" + finalResponseBodyStr);
                    }
                });
            }
        } catch (JSONException e) {
            e.printStackTrace();
            LogUtils.e("onResponse fail parse jsonobject, body=" + finalResponseBodyStr);
            MyOkHttp.mHandler.post(new Runnable() {
                @Override
                public void run() {
                    onFailure(response.code(), "fail parse jsonobject, body=" + finalResponseBodyStr);
                }
            });
        }
    }

    public void onSuccess(int statusCode, JSONObject response) {
        LogUtils.w("onSuccess(int statusCode, JSONObject response) was not overriden, but callback was received");
    }

    public void onSuccess(int statusCode, JSONArray response) {
        LogUtils.w("onSuccess(int statusCode, JSONArray response) was not overriden, but callback was received");
    }

    @Override
    public void onProgress(long currentBytes, long totalBytes) {

    }
}